//
//  LightSourceControl.swift
//  Urso
//
//  Created by Cosmetic Cygia on 2024/6/13.
//

import Foundation

protocol OptController {
    
    var ipAddress: String { get set }
    
    var port: Int { get }
    
    var timeout: Double { get set }
    
    init(ipAddress: String, port: Int)
}

class OptFlashController : OptController {
    
    var timeout: Double
    
    var ipAddress: String
    
    var port: Int
    
    var stream:URLSessionStreamTask?
    
    var onSetPulseWidthFinished: ((_ status:Bool) -> Void)?
    
    var onTriggerFinished: ((_ status:Bool) -> Void)?
    
    required init(ipAddress: String, port: Int) {
        self.ipAddress = ipAddress
        self.port = port
        self.timeout = 0
        
        if (stream != nil) {
            stream!.closeWrite()
            stream!.closeRead()
        }
        
        stream = URLSession.shared.streamTask(withHostName: ipAddress, port: port)
        if (stream != nil) {
            deprecateStreamRead(stream: stream!)
        }
    }
    
    func setLed(channel: UInt8, status: Bool) {
        var data: [UInt8] = [0xff, 0x05]
        
        data.append(channel & 0xff)
        data.append(0x00)
        data.append(status ? 0x01 : 0x00)
        
        data.append(XorCheckSum(data: data)) 
        sendUInt8(stream: stream!, dataToSend: data, timeout: timeout, escape: { [self] isSuccess, response in
            
            if (!isSuccess || (response.count != 2)) {
                ELog.d(String(describing: type(of: self)), #function, "NIMA", #line)
                PavaoManager.instance.saveLog(logLevel: LOG_LEVEL.info, logContent: "NIMA")
            }
        })
    }
    
    
    func trigger(channel: UInt8) {
        
        var data: [UInt8] = [0xff, 0x05]
        
        data.append(channel & 0xff)
        data.append(0x00)
        data.append(0x01)
        data.append(XorCheckSum(data: data))
        
        
        sendUInt8(stream: stream!, dataToSend: data, timeout: timeout, escape: { [self] isSuccess, response in
            
            if (!isSuccess || (response.count != 2)) {
                onTriggerFinished!(false)
            }
                onTriggerFinished!(((response[0] == data[3]) && (response[1] == data[4])))
        })
        
    }
    
    func setPulseWidth(channel: UInt8, pulseWidth: Int) {
        
        var data: [UInt8] = [0xff, 0x02]
        
        data.append(channel & 0xff)
        data.append(UInt8((pulseWidth / 256) & 0xff))
        data.append(UInt8((pulseWidth % 256) & 0xff))
        data.append(XorCheckSum(data: data))
        
        sendUInt8(stream: stream!, dataToSend: data, timeout: timeout, escape: { [self] isSuccess, response in
            
            if (!isSuccess || (response.count != 2)) {
                onSetPulseWidthFinished!(false)
            }
            
            onSetPulseWidthFinished!(((response[0] == data[3]) && (response[1] == data[4])))
        })
        

    }
    func setLedValue(channel: UInt8, value: Int) {
        
        var data: [UInt8] = [0xff, 0x01]
        
        data.append(channel & 0xff)
    //    data.append(UInt8((value / 256) & 0xff))
        data.append(0x00)
        data.append(UInt8((value % 256) & 0xff))
        data.append(XorCheckSum(data: data))
        
        sendUInt8(stream: stream!, dataToSend: data, timeout: timeout, escape: { [self] isSuccess,  response in
            
            if (!isSuccess || (response.count != 2)) {
                onSetPulseWidthFinished!(false)
            }
            
         //   onSetPulseWidthFinished!(((response[0] == data[3]) && (response[1] == data[4])))
        })
        

    }
}
